package cn.dengl.sky.springboot.common.config.security;

import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * spring-security登陆的密码进行MD5加密传到数据库
 */
public class CustomPasswordEncoder implements PasswordEncoder {

    /**
     *
     * @param rawPassword 输入的密码
     * @return 加盐后的加密密码，盐（hyll）
     */
    @Override
    public String encode(CharSequence rawPassword) {
        Md5PasswordEncoder encoder = new Md5PasswordEncoder();
        /*进行加盐（hyll）加密*/
        return encoder.encodePassword(rawPassword.toString(),"hyll");
    }

    /**
     *
     * @param submitPassword 密码
     * @param savePassword
     * @return
     */
    @Override
    public boolean matches(CharSequence submitPassword, String savePassword) {
        Md5PasswordEncoder encoder = new Md5PasswordEncoder();
        /*加盐后匹配*/
        /**
         * encodedPassword 数据库中密码
         *rawPassword 用户提交的密码
         */
        return encoder.isPasswordValid(savePassword,submitPassword.toString(),"hyll");
    }
}
